// share/share.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        encryptedData: '',
        url: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            encryptedData: wx.getStorageSync('encryptedData')
        })
        let This = this
        let url = decodeURIComponent(JSON.parse(
            options.data).url);
        wx.login({
            success(res) {
                if (res.code) {
                    let wxTime = new Date().getTime()
                    if (url.indexOf('?') != -1) {
                        url = `${url}&code=${res.code}&wechatType=${app.globalData.wechatType}`
                    } else {
                        url = `${url}?code=${res.code}&wechatType=${app.globalData.wechatType}`
                    }
                    url = `${url}&wxTime=${wxTime}`
                    if (This.data.encryptedData) {
                        This.setWebViewUrl(url)
                    }
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
        app.callback = res => {
            this.setWebViewUrl(url)
        }
    },
    setWebViewUrl(url) {
        wx.showLoading({
            title: '页面加载中...',
        })
        url = `${url}&encryptedData=${wx.getStorageSync('encryptedData')}&iv=${encodeURI(wx.getStorageSync('iv'))}`
        this.setData({
            encryptedData: wx.getStorageSync('encryptedData'),
            url: url
        })
        console.log(url)
        setTimeout(() => {
            wx.hideLoading()
        }, 2000)
    },
    h5PostMessage: function(e) {
        console.log(e.detail.data)
        this.h5Data = e.detail.data[e.detail.data.length - 1]; //当用户点击这个h5页面的分享时获取到
    },

    // 用户点击右上角分享
    onShareAppMessage: function(options) {
        return {
            title: this.h5Data.title,
            desc: this.h5Data.desc,
            path: this.h5Data.path
        }
    },
    methods: {
        h5Load: function() {},
        h5LoadError: function(e) {
            wx.showToast({
                title: e.detail,
                duration: 2000
            })
        }
    }
})